﻿using System.Collections.Generic;
using System.Linq;
using HeritageAcademy.Models.ViewModels;

namespace HeritageAcademy.Models.Filters
{
    public class AuthorsFilter : FilteredCourseFilter
    {
		#region Parameters 
        private List<CourseFilter> _subFilters = new List<CourseFilter>();
		#endregion Fields 

		#region Ctor 
        public AuthorsFilter(LibraryViewModel library) : base(library) { }
		#endregion

		#region Properties 
        public override string DisplayText
        {
            get { return "Авторы"; }
        }

        public override bool HasSubFilters
        {
            get { return true; }
        }

        public override List<CourseFilter> SubFilters
        {
            get { return _subFilters; }
        }
		#endregion Properties 

		#region Public Methods 
        public override void RefreshSubFilters()
        {
            var results = Library.Courses.Select(item => item.Model.Creator)
                .Distinct()
                .OrderBy(author => author);

            var filters = results.ToList().ConvertAll(authorstring => (CourseFilter)new SingleAuthorFilter(Library, authorstring));

            _subFilters = new List<CourseFilter>(filters);
        }
		#endregion Public Methods 
    }

    public class SingleAuthorFilter : CourseFilter
    {
		#region Ctor 
        public SingleAuthorFilter(LibraryViewModel library, string name)
            : base(library)
        {
            Name = name;
        }
		#endregion Constructors 

		#region Properties 
        public override string DisplayText
        {
            get { return Name; }
        }

        public override bool HasSubFilters
        {
            get { return false; }
        }

        public string Name { get; set; }
		#endregion Properties 

		#region Public Methods 
        public override void RefreshResults()
        {
            var results = Library.Courses.Where(item => item.Model.Creator.Equals(Name));
            Results = new List<CourseViewModel>(results);
        }
		#endregion Public Methods 
    }

}
